package com.txq.bilibili.app.adapter;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.txq.bilibili.app.R;
import com.txq.bilibili.app.model.RecommendContent;
import com.txq.bilibili.app.utils.FileCache;
import com.txq.bilibili.app.utils.ImageCache;
import com.txq.bilibili.app.utils.ImageLoader;

import java.util.List;

/**
 * Created by yun on 2015/3/24.
 */
public class SocialListviewAdapter extends BaseAdapter {

    private Context context;
    private List<RecommendContent> data;

    public SocialListviewAdapter(Context context,List<RecommendContent>data){

        this.context=context;
        this.data=data;
    }
    @Override
    public int getCount() {

        int ret=0;
        if(data!=null){

            ret=data.size();
        }
        return ret;
    }

    @Override
    public Object getItem(int position) {

        Object ret=null;
        if(data!=null){

            ret=data.get(position);
        }
        return ret;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view=null;

        if(convertView!=null){
            view=convertView;
        }else{
            view=LayoutInflater.from(context).inflate(R.layout.fragment_social_list_view_item,parent,false);

        }
        ViewHolder holder=(ViewHolder)view.getTag();
        if(holder==null){
            holder=new ViewHolder();

            holder.imageTitle=(ImageView)view.findViewById(R.id.social_list_vie_advice_img);
            holder.textViewTitle=(TextView)view.findViewById(R.id.social_list_view_txt1);
            holder.txtContent=(TextView)view.findViewById(R.id.social_list_view_item_sub_title);
            holder.txtToday=(TextView)view.findViewById(R.id.social_list_view_txt_today);

            view.setTag(holder);
        }

        RecommendContent recommendContent=data.get(position);

        Log.d("....listview",recommendContent.getTitle()+"  "+recommendContent.getContentWithoutPics());
        holder.textViewTitle.setText(recommendContent.getTitle());
        holder.txtContent.setText(recommendContent.getContentWithoutPics());
        holder.txtToday.setText("今日"+recommendContent.getnReplies());


        String url=recommendContent.getPicUrl();
        if(url!=null && url.length()>0){
            holder.imageTitle.setTag(url);
            holder.imageTitle.setImageResource(R.drawable.ic_launcher);

          /*  Resources resources = context.getResources();
            Drawable drawable=resources.getDrawable(R.drawable.social_fragment_list_view_img_bg);
            holder.imageTitle.setBackground(drawable);*/
            ImageCache imageCache = ImageCache.getInstance();
            Bitmap bitmap = imageCache.getImage(url);
            if(bitmap != null){
                holder.imageTitle.setImageBitmap(bitmap);
            }else {
                FileCache fileCache = FileCache.createInstance(context);
                byte[] bytes = fileCache.getContent(url);
                if(bytes != null && bytes.length>0){
                    Bitmap bmp = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
                    holder.imageTitle.setImageBitmap(bmp);
                    imageCache.putImage(url,bmp);
                }else {
                    //寮傛鍔犺浇
                    ImageLoader task = new ImageLoader(holder.imageTitle);
                    task.execute(url);
                }
            }

        }



        return view;
    }

    public class ViewHolder{
        private ImageView imageTitle;
        private TextView textViewTitle;
        private TextView txtToday;
        private TextView txtContent;


    }
}
